package com.jeewms.www.tms.ui.acitivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.base.BaseBackActivity;
import com.jeewms.www.tms.bean.bean.MessageEvent;
import com.jeewms.www.tms.bean.vm.BaseVm;
import com.jeewms.www.tms.bean.vm.DemoVm;
import com.jeewms.www.tms.constance.ClassHolder;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.ui.adapter.DemoAdapter;
import com.jeewms.www.tms.util.ClickFastUtil;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.LoadingUtil;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.SharedPreferencesUtil;
import com.jeewms.www.tms.util.ToastUtil;
import com.jeewms.www.tms.util.UUIDUtil;
import com.jeewms.www.tms.util.UpdataUiUtil;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/8/5.
 */

public class ShangjiaDetailActivity extends BaseBackActivity {


    @BindView(R.id.tv_x1)
    TextView tvX1;
    @BindView(R.id.et_x1)
    EditText etX1;
    @BindView(R.id.ll_x1)
    PercentLinearLayout llX1;
    @BindView(R.id.div_x1)
    ImageView divX1;
    @BindView(R.id.tv_x2)
    TextView tvX2;
    @BindView(R.id.et_x2)
    EditText etX2;
    @BindView(R.id.ll_x2)
    PercentLinearLayout llX2;
    @BindView(R.id.div_x2)
    ImageView divX2;
    @BindView(R.id.tv_x3)
    TextView tvX3;
    @BindView(R.id.et_x3)
    EditText etX3;
    @BindView(R.id.ll_x3)
    PercentLinearLayout llX3;
    @BindView(R.id.div_x3)
    ImageView divX3;
    @BindView(R.id.tv_x4)
    TextView tvX4;
    @BindView(R.id.et_x4)
    EditText etX4;
    @BindView(R.id.ll_x4)
    PercentLinearLayout llX4;
    @BindView(R.id.div_x4)
    ImageView divX4;
    @BindView(R.id.tv_x5)
    TextView tvX5;
    @BindView(R.id.et_x5)
    EditText etX5;
    @BindView(R.id.ll_x5)
    PercentLinearLayout llX5;
    @BindView(R.id.div_x5)
    ImageView divX5;
    @BindView(R.id.tv_x6)
    TextView tvX6;
    @BindView(R.id.et_x6)
    EditText etX6;
    @BindView(R.id.ll_x6)
    PercentLinearLayout llX6;
    @BindView(R.id.div_x6)
    ImageView divX6;
    @BindView(R.id.tv_x7)
    TextView tvX7;
    @BindView(R.id.et_x7)
    EditText etX7;
    @BindView(R.id.div_x8)
    ImageView divX8;
    @BindView(R.id.tv_x8)
    TextView tvX8;
    @BindView(R.id.et_x8)
    EditText etX8;
    @BindView(R.id.ll_x8)
    PercentLinearLayout llX8;
    @BindView(R.id.ll_x7)
    PercentLinearLayout llX7;
    @BindView(R.id.div_x7)
    ImageView divX7;
    @BindView(R.id.tv_xiayixiang)
    TextView tvXiayixiang;
    @BindView(R.id.lv_list)
    ListView lvList;
    @BindView(R.id.zhanyongchuwei)
    TextView zhanyongchuwei;
    DemoAdapter adapter;
    private String className;
    private String type;
    private int position;
    private DemoVm vm;

    public static void show(Activity context, String type, String className, DemoVm vm, int position) {
        Intent intent = new Intent(context, ShangjiaDetailActivity.class);
        intent.putExtra("className", className);
        intent.putExtra("type", type);
        intent.putExtra("position", position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("vm", (Serializable) vm);
        intent.putExtras(bundle);
        context.startActivityForResult(intent, 0);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent msg) {
        if (msg.getKey().equals("key2")) {
            ShangjiaDetailActivity.show(this, type, className, msg.getVm(), msg.getMessage4());
            finish();
        } else if (msg.getKey().equals("key3")) {
            ToastUtil.show(this, "这是最后一项了");
        } else if (msg.getKey().equals("key5")) {
            new Handler().postDelayed(new Runnable() {//定义延时任务模仿网络请求
                @Override
                public void run() {
                    finish();
                }
            }, 700);
        }
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        className = getIntent().getStringExtra("className");
        type = getIntent().getStringExtra("type");
        position = getIntent().getIntExtra("position", 0);
        vm = (DemoVm) getIntent().getSerializableExtra("vm");
      //  vm1 = (DemoVm) getIntent().getSerializableExtra("vm1");
        setTitle(className);
        initTile();
        ((TextView) mBtnRight).setText("保存");
        mBtnRight.setVisibility(View.VISIBLE);
        initAdapter();
    }

    private void initTile() {
        if (className.equals(ClassHolder.SHANGJIADetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4,llX5, divX5, llX6, divX6).
                    hideViews( llX7, divX7)
                    .setEnadle(etX1, etX2)
                    .setText("交接单号：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("上架储位：", tvX3)
                    .setText("良品数：", tvX4)
                    .setText("库位：", tvX5)
                    .setText("不良品数：", tvX6)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX6(), etX3)
                    .setText(vm.getWmX17(), etX4)
                    .setText(vm.getWmX9(), etX5)
                    .setText("", etX6);
            etX4.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
//                   etX3.setCursorVisible(true);
                  etX3.requestFocus();


        } else if (className.equals(ClassHolder.CHENGPINXIAJIADetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4,divX5,llX5).
                    hideViews( llX6, divX6, llX7, divX6)
                   .setEnadle(etX1,etX2,etX5)
                 //   .setText("物料号：", tvX1)
                   // .setText("你好",tvX1)
                    .setText("交货单：", tvX2)
                    .setText("下架储位：", tvX3)
                    .setText("数量：", tvX4)
                    .setText("物料号：", tvX5)
                    .setText(vm.getWmX2(), etX1)
                    .setText(vm.getWmX5(), etX2)
                    .setText(vm.getWmX2(),etX5)
                 //   .setText(vm.getWmX4(), etX3)
                    .setText(vm.getWmX17(), etX4);
            etX4.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
                  etX3.requestFocus();
        }
        if (className.equals(ClassHolder.YLSJDetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4, llX5, divX5, llX6, divX6).
                    hideViews(llX7, divX7)
                    .setEnadle(etX1, etX2)
                    .setText("交接单号：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("上架储位：", tvX3)
                    .setText("良品数：", tvX4)
                    .setText("库位：", tvX5)
                    .setText("不良品数：", tvX6)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX8(), etX3)
                    .setText(vm.getWmX17(), etX4)
                    .setText(vm.getWmX13(), etX5)
                    .setText("", etX6);
            etX4.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
//                   etX3.setCursorVisible(true);
            etX3.requestFocus();
        }
        else if (className.equals(ClassHolder.FAHUOXIAJIADetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4).
                    hideViews(llX6, divX6, llX7, divX7)
                    .setEnadle(etX1, etX2)
                    .setText("快递单号：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("下架储位：", tvX3)
                    .setText("数量：", tvX4)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX6(), etX3)
                    .setText(vm.getWmX17(), etX4);
        }
     //JIT发货
        else if (className.equals(ClassHolder.JITDetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4,llX5,divX5).
                    hideViews(llX6, divX6, llX7, divX7)
                    .setEnadle(etX1, etX2)
                    .setText("快递单号：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("下架储位：", tvX3)
                    .setText("箱号：", tvX5)   // 增加 唯品会箱号
                    .setText("数量：", tvX4)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX6(), etX3)
                    .setText("",etX5)
                    .setText(vm.getWmX17(), etX4);
        }

        else if (className.equals(ClassHolder.CAIGOUTUIHUODetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4, llX5, divX5, llX6, divX6, llX7, divX7,llX8, divX8)
                    .setEnadle(etX1, etX2, etX3)
                    .setText("物料凭证：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("物料描述：", tvX3)
                    .setText("上架储位：", tvX4)
                    .setText("上架数量：", tvX5)
                    .setText("库位：", tvX6)
                    .setText("退货数量：", tvX7)
                    .setText("批次：", tvX8)

                    .setText(vm.getWmX1(), etX1) //ID
                    .setText(vm.getWmX2(), etX2)//类型
                    .setText(vm.getWmX4(), etX3)
                 //   .setText(vm.getWmX6(), etX4);
                     .setText(vm.getWmX17(), etX5)
                     .setText(vm.getWmX14(), etX6)
                      .setText(vm.getWmX9(), etX8);
            etX5.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
            etX7.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
//            etX3.setCursorVisible(true);
                 etX4.requestFocus();
             //  etX3.setSelection(3);
                  //  .setText(vm.getWmX7(), etX7);
        } else if (className.equals(ClassHolder.XIAJIADetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4, llX5, divX5, llX6, divX6).
                    hideViews(llX7, divX7)
                    .setEnadle(etX1, etX2, etX3, etX4)
                    .setText("物料凭证：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("物料描述：", tvX3)
                    .setText("库位：", tvX4)
                    .setText("下架储位：", tvX5)
                    .setText("数量：", tvX6)
                 //   .setText("批次：", tvX7)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX4(), etX3)
                    .setText(vm.getWmX5(), etX4)
                    .setText(vm.getWmX8(), etX5)
                    .setText(vm.getWmX17(), etX6);
            etX5.requestFocus();
            //设置数值字段值允许输入数字和小数位.modify by it_yeys.
            etX6.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
            //设置数值字段值允许输入数字和小数位.modify by it_yeys.
        } else if (className.equals(ClassHolder.KUCUNCHAXUNDetail)) {

        } else if (className.equals(ClassHolder.PANDIANDetail)) {

        } else if (className.equals(ClassHolder.YICHUDetail)) {

        } else if (className.equals(ClassHolder.YIKUDetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4, llX5, divX5, llX6, divX6, llX7, divX7)
                    .setEnadle(etX1, etX2, etX3)
                    .setText("物料号：", tvX1)
                    .setText("物料描述：", tvX2)
                    .setText("源库位：", tvX3)
                    .setText("源储位：", tvX4)
                    .setText("数量：", tvX5)
                    .setText("目标库位：", tvX6)
                    .setText("目标储位：", tvX7)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText(vm.getWmX3(), etX3)
                    .setText(vm.getWmX4(), etX4)
                    .setText(vm.getWmX5(), etX5)
                    .setText(vm.getWmX6(), etX6)
                    .setText(vm.getWmX17(), etX7);
        } else if (className.equals(ClassHolder.CHENGPINTUIHUODetail)) {
            UpdataUiUtil.getDefaul().showViews(llX1, divX1, llX2, divX2, llX3, divX3, llX4, divX4, llX5, divX5, llX6, divX6)
                    .setEnadle(etX1, etX2,etX4)
                    .setText("退货小票：", tvX1)
                    .setText("物料号：", tvX2)
                    .setText("上架储位：", tvX3)
                    .setText("总数量：", tvX4)
                    .setText("良品数：", tvX5)
                    .setText("不良品数：", tvX6)
                    .setText(vm.getWmX1(), etX1)
                    .setText(vm.getWmX2(), etX2)
                    .setText("", etX3)
                    .setText(vm.getWmX17(), etX4)
                    .setText(vm.getWmX17(), etX5)
                    .setText("", etX6);
            etX3.requestFocus();
            etX6.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
            etX5.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
    }


    private void initAdapter() {
        adapter = new DemoAdapter(getActivity());
        adapter.setType(1);
        lvList.setAdapter(adapter);
        adapter.setWmi1List(vm.getWmi1List());
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
        if(ClickFastUtil.isFastClick1000())
        save();
    }

    private void save() {
        LoadingUtil.showLoading(this);
        Logutil.print("执行保存动作");
        Map<String, String> map = new HashMap<>();
        map.put("wxs1", UUIDUtil.getUUID32().toUpperCase());//uuid
        map.put("createBy", SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME));
        map.put("wxs3", etX1.getText().toString());//单号

        if (type.equals("LLSJ")){//"打码入库";
            //判断输入的下架数量不允许大于待下架数量.20180918.
//        .setText("交接单号：", tvX1)
//                    .setText("物料号：", tvX2)
//                    .setText("上架储位：", tvX3)
//                    .setText("良品数：", tvX4)
//                    .setText("库位：", tvX5)
//                    .setText("不良品数：", tvX6)
            Double shouhuocount =0.00;
            Double tuihuocount = 0.00;
            z_toast t_dm = new z_toast();
            try{
                shouhuocount =   Double.parseDouble(etX4.getText().toString());
            }catch (Exception e){
            }
            try{
                tuihuocount =   Double.parseDouble(etX6.getText().toString());
            }catch (Exception e){
            }
            if((shouhuocount + tuihuocount)<=0){
                t_dm.showToast("请输入数量！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (shouhuocount>0&&TextUtils.isEmpty(etX3.getText()) ){
                //    LoadingUtil.hideLoading();
                t_dm.showToast("储位不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (shouhuocount>0&&TextUtils.isEmpty(etX5.getText())){
                //    LoadingUtil.hideLoading();
                t_dm.showToast("库位不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (Double.parseDouble(vm.getWmX17().toString())< (shouhuocount + tuihuocount)){

                t_dm.showToast("上架数量不允许超过待上架数量!",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
              return;
            }
            map.put("wxs41", vm.getWmX1());//雷射订单号
            map.put("wxs42", vm.getWmX2());//联合主键
            map.put("wxs43", vm.getWmX8());//雷射订单行项目
            map.put("wxs21", vm.getWmX7());//生产订单号
            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
            map.put("wxs6", etX4.getText().toString());//良数量

            map.put("wxs33", etX6.getText().toString());//不良数量

            map.put("wxs9", etX5.getText().toString());//库位

        }else   if(type.equals("PZXJ")){//"下架";

            if (TextUtils.isEmpty(etX5.getText().toString())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
              //  Logutil.print(vm.getWmX5().toString());
                t_dm.showToast("储位不能为空！",ShangjiaDetailActivity.this);
                //  LoadingUtil.hideLoading();
                return;
            }
            if (TextUtils.isEmpty(etX6.getText())){
                z_toast t_dm = new z_toast();
                //    LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (Double.parseDouble(etX6.getText().toString())<= 0.00){
                z_toast t_dm = new z_toast();
                //    LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能<=0!",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }



            if (Double.parseDouble(vm.getWmX17().toString())< Double.parseDouble(etX6.getText().toString())){
                z_toast t_dm = new z_toast();
                t_dm.showToast("下架数量"+etX6.getText().toString()+"不允许超过待下架数量"+ vm.getWmX17(),ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (TextUtils.isEmpty(vm.getWmX5())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                Logutil.print(vm.getWmX5().toString());
                t_dm.showToast("储位不能为空！",ShangjiaDetailActivity.this);
                //  LoadingUtil.hideLoading();
                return;
            }
            map.put("wxs41", vm.getWmX1());//物料凭证
            map.put("wxs42", vm.getWmX11());//物料号
            map.put("wxs43", vm.getWmX12());//物料描述
            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX5.getText().toString());//储位
            map.put("wxs6", etX6.getText().toString());//上架数量
            // map.put("wxs7", etX5.getText().toString());


        } else if (type.equals("PZSJ")) {//="采购入库";
            Double shouhuocount =0.00;
            Double tuihuocount = 0.00;
            z_toast t_dm = new z_toast();
            try{
                shouhuocount =   Double.parseDouble(etX5.getText().toString());
            }catch (Exception e){
            }
            try{
                tuihuocount =   Double.parseDouble(etX7.getText().toString());
            }catch (Exception e){
            }
            if((shouhuocount + tuihuocount)<=0){
                t_dm.showToast("请输入数量！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if(!"103".equals(vm.getWmX15())&&tuihuocount>0){
                t_dm.showToast("当前移动类型不允许退货！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (TextUtils.isEmpty(etX4.getText())&& shouhuocount > 0.00){
                t_dm.showToast("货架号不能为空！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (TextUtils.isEmpty(etX6.getText())&& shouhuocount > 0.00){
                 t_dm.showToast("库位不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (Double.parseDouble(vm.getWmX17().toString()) < (shouhuocount + tuihuocount) ) {
                t_dm.showToast("数量大于待上架数量!", ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (!TextUtils.isEmpty(vm.getWmX9())) {
                if (TextUtils.isEmpty(etX8.getText().toString())&& shouhuocount > 0.00) {
                    t_dm.showToast("启用批次物料，请输入批次", ShangjiaDetailActivity.this);
                    return;
                }
            }
            map.put("wxs41", vm.getWmX1());//物料凭证
            map.put("wxs42", vm.getWmX11());//年份
            map.put("wxs43", vm.getWmX12());//物料凭证行项目
            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX4.getText().toString());//储位
            map.put("wxs6", etX5.getText().toString());//上架数量
            map.put("wxs7", etX7.getText().toString());//退货数量
            map.put("wxs8", vm.getWmX15());//移动类型
            map.put("wxs9", etX6.getText().toString());//库位
            map.put("wxs31", etX8.getText().toString());//批次

        }else if(type.equals(ClassHolder.YIKU2)){//"移库2";

        }//预留单过账。
        else if (type.equals("YLYC")){ // ----------------------------预留上架.
            Double shouhuocount =0.00;
            Double tuihuocount = 0.00;
            z_toast t_dm = new z_toast();
            try{
                shouhuocount =   Double.parseDouble(etX4.getText().toString());
            }catch (Exception e){
            }
            try{
                tuihuocount =   Double.parseDouble(etX6.getText().toString());
            }catch (Exception e){
            }
            if((shouhuocount + tuihuocount)<=0){
                t_dm.showToast("请输入数量！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (shouhuocount>0&&TextUtils.isEmpty(etX3.getText())){
                //    LoadingUtil.hideLoading();
                t_dm.showToast("储位不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (shouhuocount>0&&TextUtils.isEmpty(etX5.getText())){
                //    LoadingUtil.hideLoading();
                t_dm.showToast("库位不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (Double.parseDouble(vm.getWmX17().toString())< (shouhuocount + tuihuocount)){

                t_dm.showToast("上架数量不允许超过待上架数量!",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            map.put("wxs41", vm.getWmX1());//预留单号
            map.put("wxs42", vm.getWmX2());//联合主键
            map.put("wxs43", vm.getWmX14());//预留单行项目号
          //  map.put("wxs21", vm.getWmX7());//生产订单号
            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
            map.put("wxs6", etX4.getText().toString());//良数量
            map.put("wxs7", etX6.getText().toString());//不良数量
            map.put("wxs11", "B901");//不良库位
            map.put("wxs9","B109");//良品库位
            map.put("wxs10", etX5.getText().toString());//良品储位




        }

        else if(type.equals("JHXJ")){//"成品拣货";
//            Logutil.print(""+vm.getWmX1());
//            Logutil.print("type"+type);
            if (Double.parseDouble(vm.getWmX17().toString())<= 0.00){
                z_toast t_dm = new z_toast();
               LoadingUtil.hideLoading();
                t_dm.showToast("不允许超发!",ShangjiaDetailActivity.this);
               // LoadingUtil.hideLoading();return;
                return;
            }

            if (TextUtils.isEmpty(etX4.getText())){
                z_toast t_dm = new z_toast();
                t_dm.showToast("数量不能为空",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (TextUtils.isEmpty(etX3.getText())){
                z_toast t_dm = new z_toast();
                t_dm.showToast("仓位不能为空！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

//                   //点击保存按钮 检查主要数据是否为空，为空Toast 不能继续.
            if (!TextUtils.isEmpty(etX4.getText())) {
                if (Double.parseDouble(etX4.getText().toString())<=0) {
                    z_toast t_dm = new z_toast();
                    t_dm.showToast("数量不能为0",ShangjiaDetailActivity.this);
                    LoadingUtil.hideLoading();
                    return;
                }

                if (Double.parseDouble(vm.getWmX17().toString()) < Double.parseDouble(etX4.getText().toString())) {
                    z_toast t_dm = new z_toast();
                    t_dm.showToast("下架数量大于待下架数量!", ShangjiaDetailActivity.this);
                    LoadingUtil.hideLoading();
                    return;
                }
            }
            map.remove("wxs3");
            map.put("wxs3", vm.getWmX1());//单号
            map.put("wxs41", vm.getWmX1());//装箱清单号/交货单号
            map.put("wxs42", vm.getWmX2());//物料号
            map.put("wxs43", vm.getWmX7());//交货单/装箱清单行项目
          //  map.put("wxs43", vm.getWmX7());//交货单/装箱清单行项目
            map.put("wxs21", vm.getWmX5());//交货单

            map.put("wxs4", etX1.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
            map.put("wxs6", etX4.getText().toString());//上架数量
        }else if(type.equals("DSXJ")){//"电商发货";
            Logutil.print("上架数量是否为"+vm.getWmX17());
            if (TextUtils.isEmpty(etX4.getText())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能为空！",ShangjiaDetailActivity.this);
                return;
            }
            if (Double.parseDouble(etX4.getText().toString())<= 0.00){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能<=0!",ShangjiaDetailActivity.this);
                return;
            }
            if (Double.parseDouble(vm.getWmX17().toString())< Double.parseDouble(etX4.getText().toString())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能大于待下架数量",ShangjiaDetailActivity.this);
                return;
            }


            if (!TextUtils.isEmpty(vm.getWmX11())){
                z_toast t_dm1 = new z_toast();
                LoadingUtil.hideLoading();
                t_dm1.showToast("料号有条码请在主页面发货!",ShangjiaDetailActivity.this);
                return;
            }



            Logutil.print("WXS6的值"+etX5.getText().toString());

            map.put("wxs41", vm.getWmX1());//快递单号
            map.put("wxs42", vm.getWmX2());//物料号
            map.put("wxs43", vm.getWmX4());//or_sn

            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
           map.put("wxs6", etX4.getText().toString());//上架数量
         //   map.put("wxs6", vm.getWmX17());//上架数量
        }
        else if(type.equals("JIT")){//"唯品会电商发货";
            Logutil.print("上架数量是否为"+vm.getWmX17());

            if (TextUtils.isEmpty(etX5.getText())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("唯品会箱号不能为空！",ShangjiaDetailActivity.this);
                return;
            }




            if (TextUtils.isEmpty(etX4.getText())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能为空！",ShangjiaDetailActivity.this);
                return;
            }
            if (Double.parseDouble(etX4.getText().toString())<= 0.00){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能<=0!",ShangjiaDetailActivity.this);
                return;
            }
            if (Double.parseDouble(vm.getWmX17().toString())< Double.parseDouble(etX4.getText().toString())){
                z_toast t_dm = new z_toast();
                LoadingUtil.hideLoading();
                t_dm.showToast("发货数量不能大于待下架数量",ShangjiaDetailActivity.this);
                return;
            }

//JIT 的临时注销 2019、/0/16   JIT发货PK 取消条码管理
            if (!TextUtils.isEmpty(vm.getWmX11())){
                z_toast t_dm1 = new z_toast();
                Logutil.print(vm.getWmX11().toString());
                LoadingUtil.hideLoading();
                t_dm1.showToast("料号有条码请在主页面发货!",ShangjiaDetailActivity.this);
                return;
            }



            Logutil.print("WXS6的值"+etX5.getText().toString());

            map.put("wxs41", vm.getWmX1());//快递单号
            map.put("wxs42", vm.getWmX2());//物料号
            map.put("wxs43", vm.getWmX7());//or_sn
            map.put("wxs21",vm.getWmX5());//交货单号
            map.put("wxs39",etX5.getText().toString());//唯品会箱号

            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
            map.put("wxs6", etX4.getText().toString());//上架数量
            //   map.put("wxs6", vm.getWmX17());//上架数量
        }






























        else if(type.equals("CPTH")){//"成品退货";


            Double shouhuocount =0.00;
            Double tuihuocount = 0.00;
            z_toast t_dm = new z_toast();
            try{
                shouhuocount =   Double.parseDouble(etX5.getText().toString());
            }catch (Exception e){
            }
            try{
                tuihuocount =   Double.parseDouble(etX6.getText().toString());
            }catch (Exception e){
            }
            if((shouhuocount + tuihuocount)<=0){
                t_dm.showToast("请输入数量！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }
            if (TextUtils.isEmpty(etX3.getText())&& shouhuocount > 0.00){
                t_dm.showToast("货架号不能为空！",ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            if (Double.parseDouble(vm.getWmX17().toString()) < (shouhuocount + tuihuocount) ) {
                t_dm.showToast("数量大于待上架数量!", ShangjiaDetailActivity.this);
                LoadingUtil.hideLoading();
                return;
            }

            map.put("wxs4", etX2.getText().toString());//物料号
            map.put("wxs5", etX3.getText().toString());//储位
            map.put("wxs6", etX5.getText().toString());//上架数量
            map.put("wxs7", etX6.getText().toString());//退货数量
            map.put("wxs8", vm.getWmX4());//退货单号

            map.put("wxs41", vm.getWmX1());//退货单号
            map.put("wxs42", vm.getWmX2());//物料号
            map.put("wxs43", vm.getWmX2());//物料号
        }

        //判断下架的详细页面，传输物料号、库位、货架号取库存，如果库存不存在或者库存<发料数量，totast 不允许下架.
     //   String url = Constance.gettSapLsqdControllerURL() + "?username=" + SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME);
       String url1 = "http://192.168.200.101:8080/zzwms/rest/wmapi/getdoc/KCCX"+"?searchstr2="+vm.getWmX2()+"&searchstr3="+etX6.getText().toString()
                     + "&searchstr4=" + vm.getWmX9() + "&searchstr5=" + etX4.getText().toString();
        Logutil.print(url1);
        String arr = "";
        JSONObject jsonObject = new JSONObject(map);
        String str = "";
        str = jsonObject.toString();
        String url = Constance.getwmSttInGoodsControllercURL() + "?doctype=" + type + "&tSapLtttstr=" + str;
        Logutil.print("打印Map_url===" + url+str);
        HTTPUtils.get(this, url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LoadingUtil.hideLoading();
                EventBus.getDefault().post(new MessageEvent("key4", position));
                ToastUtil.show(getActivity(), "未知错误");
            }

            @Override
            public void onResponse(String response) {
                LoadingUtil.hideLoading();
                BaseVm vm = GsonUtils.parseJSON(response, BaseVm.class);
                Logutil.print("返回数据"+vm.isOk());
                if (vm.isOk()) {
                    ToastUtil.show(getActivity(), "保存成功");
                    EventBus.getDefault().post(new MessageEvent("key4", position));
                } else {
                    if (TextUtils.isEmpty(vm.getErrorMsg())){
                        z_toast t_dm1 = new z_toast();
                        LoadingUtil.hideLoading();
                        t_dm1.showToast("保存失败",ShangjiaDetailActivity.this);
                        return;
                    }else
                    {
                    ToastUtil.show(getActivity(), vm.getErrorMsg());}
                    //保存失败！
                }
            }
        });
    }


    @Override
    protected int getContentResId() {
        return R.layout.activity_shangjiadetail;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_xiayixiang)
    public void onViewClicked() {
        EventBus.getDefault().post(new MessageEvent("key1", position));
    }
}
